package com.rfid.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.entitymapper.driver.DriverManager;
import com.entitymapper.driver.impl.RFIDDriver;
import com.rfid.RFIDCallException;
import com.rfid.listener.LogListener;
import com.rfid.listener.SettingListener;
import com.rfid.listener.StartListener;
import com.rfid.listener.StopListener;

/**
 * 读监听客户端
 * @author hhf
 */
public class Client extends JFrame{
	
	private JButton start = new JButton("开启");
	private JButton stop = new JButton("停止");
	private JButton setting	 = new JButton("设置");
	
	private SettingWindow setttingWindow;
	
	private JList logList = new ListenningLogEventJList();
	
	/**
	 * 记录日志
	 * @author hhf
	 */
	private class ListenningLogEventJList extends JList implements LogListener {

		private static final long serialVersionUID = -2372682529784801082L;
		@SuppressWarnings("rawtypes")
		private Queue dataQueue = new LinkedList();
		private int size = 5;
		
		@SuppressWarnings({ "unchecked", "rawtypes" })
		@Override
		public void onLogEvent(LogEvent obj) {
			if(dataQueue.size() > size) {
				dataQueue.poll();
			}
			dataQueue.add(obj.getId() + " : "+obj.getName());
			Collections.reverse((List)dataQueue);
			this.setListData(dataQueue.toArray());
			Collections.reverse((List)dataQueue);
		}
	}

	private static final long serialVersionUID = 1L;

	public Client() {
		
		initCmp();
		initCmpListeners();
	}
	
	private void initCmp() {
		
		JPanel buttonPanel = new JPanel();
		JPanel panel = new JPanel();
		JPanel logPanel = new JPanel();
		
		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		JPanel buttomPanel = new JPanel();
		JScrollPane scrollPane = new JScrollPane(logList);
		panel.setLayout(new BorderLayout(2,1));
		buttonPanel.add(start);
		buttonPanel.add(stop);
		buttonPanel.add(setting);
		
		logPanel.setLayout(new GridLayout(1,1));
		logPanel.add(scrollPane);
		panel.add(buttonPanel,BorderLayout.NORTH);
		panel.add(logPanel,BorderLayout.CENTER);

		panel.add(leftPanel,BorderLayout.WEST);
		panel.add(rightPanel,BorderLayout.EAST);
		panel.add(buttomPanel,BorderLayout.SOUTH);
		this.setttingWindow = new SettingWindow(this, true);
		this.add(panel);
	}
	
	private void initCmpListeners() {
		StartListener startListener = new StartListener();
		startListener.setListCmp((LogListener)logList);
		start.addActionListener(startListener);
		ActionListener stopListener = new StopListener();
		stop.addActionListener(stopListener);
		ActionListener settingListener = new SettingListener(this);
		setting.addActionListener(settingListener); 
	}
	public JButton getStart() {
		return start;
	}
	public void setStart(JButton start) {
		this.start = start;
	}
	public JButton getStop() {
		return stop;
	}
	public void setStop(JButton stop) {
		this.stop = stop;
	}
	public JList getLogList() {
		return logList;
	}
	public void setLogList(JList logList) {
		this.logList = logList;
	}

	public static void main(String[] args) throws RFIDCallException {
		
		RFIDDriver driver = new RFIDDriver();
		DriverManager.registerDriver(driver);
		Client client = new Client();
		client.setTitle("RFID 客户端");
		client.setLocationRelativeTo(null);
		client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		client.setSize(400, 300);
		client.setVisible(true);
		
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = (int) (screenSize.getWidth()/2);
		int screenHight = (int) (screenSize.getHeight()/2);
		int width = client.getWidth();
		int hight = client.getHeight();
		client.setLocation(screenWidth-width/2, screenHight-hight/2);
		
	}

	/**
	 * @return the setClient
	 */
	public SettingWindow getSetClient() {
		return setttingWindow;
	}
	
	
}
